<?php
class Hero extends AbstractCharacter
{
        public $characterTalents;
    
	public function __construct($id)
	{
		$this->characterData["id"] = $id;
		$this->loadData();
	}

	public function __wakeup()
	{
		$this->loadData();
	}

        public function __set($name, $value)
	{
		$this->characterData[$name] = $value;
                
                dibi::query("UPDATE hero SET $name = '$value' WHERE id = %i", $this->id);
	}
        
	private function loadData()
	{
                // načítá data o hrdinovi
                $s = dibi::query("SELECT *
                                    FROM hero
                                   WHERE id = %i", $this->id);
                $r = $s->fetchAll();

                $races = getInum("RACE");
                
                $this->characterData = array("id"           => $r[0]->id,
                                             "name"         => $r[0]->name,
                                             "race"         => $r[0]->race,
                                             "raceName"     => $races[$r[0]->race],
                                             "sex"          => $r[0]->pohlavi,
                                             "gold"         => $r[0]->gold,
                                             "level"        => $r[0]->level,
                                             "exp"          => $r[0]->exp,
                                             "avatar"       => $r[0]->avatar,
                                             "talents"      => $r[0]->talents,
                                             "strength"     => $r[0]->strength,
                                             "dexterity"    => $r[0]->dexterity,
                                             "intelligence" => $r[0]->intelligence,
                                             "vitality"     => $r[0]->vitality,
                                             "wisdom"       => $r[0]->wisdom,
                                             "hp"           => $r[0]->hp,
                                             "mana"         => $r[0]->mana);
                
                $this->characterData["attackMin"] = $this->strength;
                $this->characterData["attackMax"] = $this->strength;
                
                /*
                // načíst talenty                
                $s = dibi::query("SELECT talent, level
                                    FROM hero_talents
                                   WHERE hero = %i", $this->id);
                $r = $s->fetchAll();
                foreach ($r AS $v)
                {
                        $this->characterTalents[$v->talent] = $v->level;
                }
                */
                
                // ovlivnění atributů talenty
                //$this->characterData["vitality"] += $this->getTalentLevel(12);
                //$this->characterData["hp"] = $this->hp+($val*3);
                //$this->characterData["wisdom"] += $this->getTalentLevel(18);
                //$this->characterData["mana"] = $this->mana+($val*2);
                
                // načíst nasazené předměty -> ovlivnění atributů
                $s = dibi::query("SELECT i.strength, i.dexterity, i.intelligence, i.vitality, i.wisdom, i.attack_min AS 'attackMin', i.attack_max AS 'attackMax'
                                    FROM items i, hero_person p
                                   WHERE i.id = p.item
                                     AND p.hero = %i", $this->id);
                $r = $s->fetchAll();
                foreach ($r as $v)
                {
                        foreach ($v as $name => $val)
                        {
                                if ($name == "vitality")
                                {
                                        $this->characterData["hp"] = $this->hp+($val*3);
                                }
                                else if ($name == "wisdom")
                                {
                                        $this->characterData["mana"] = $this->mana+($val*2);
                                }
                                
                                $this->characterData[$name] += $val;
                        }
                }  
                
                $this->characterData["hpMax"] = $this->countHP();
                $this->characterData["manaMax"] = $this->countMana();
                
                // uložit datum posledního načtení 
                $cas = Time();
                dibi::query("UPDATE account SET online = '1', last_access = '$cas' WHERE id = %i", $this->id);
	}	
        
        public function addVitality()
        {
                
        }

        public function countHP()
        {
                return $this->vitality * 3;
        }
        
        public function countMana()
        {
                return $this->wisdom * 2;
        }
        
        public function getTalentLevel($id)
        {
                return (isset($this->characterTalents[$id]) ? $this->characterTalents[$id] : 0);
        }
        
	public function safeHero()
	{
		global $data;
		$data["hero"] = serialize($this);
	}
        
        public function hasTalent($id)
        {
                if (isset($this->characterTalents[$id]))
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }
        
        public function getTalentReq($id)
        {
                $buff = array();
                $check = true;
                
                // nejprve kontrolujeme, jestli na to má dost bodů v hlavním směru
                $s = dibi::query("SELECT w.way, w.pointsinway, w.pointsmax, mainW.name
                                    FROM ways_talents w
                                    JOIN ways_talents mainW ON (w.way = mainW.id)
                                   WHERE w.id = %i", $id);
                $r = $s->fetchAll();
                $way = $r[0]->way;
                $pointsinway = $r[0]->pointsinway;
                $pointsmax = $r[0]->pointsmax;
                
                // má dost bodů v hlavním směru?
                if ($pointsinway > 0 && (!isset($this->characterTalents[$way]) OR $this->characterTalents[$way] < $pointsinway))
                {
                        $buff[] = "Vyžaduje " . $pointsinway . " bodů v <b>" . $r[0]->name . "</b>";
                        $check = false;
                }
                
                // kontrolujeme rekurzivně jednotlivé předchozí nutné talenty
                $lasttalent = 0;
                while (true)
                {
                        $s = dibi::query("SELECT d.mother, w.pointsmax, w.name
                                            FROM ways_talents_dependence d, ways_talents w
                                           WHERE d.talent = %i", ($lasttalent == 0 ? $id : $lasttalent),
                                            "AND d.mother = w.id");
                        $r = $s->fetchAll();
                        if (empty($r))
                        {
                            break;
                        }
                        
                        $lasttalent = $r[0]->mother;
                        $pointsmax = $r[0]->pointsmax;
                        
                        if (!isset($this->characterTalents[$lasttalent]) OR $this->characterTalents[$lasttalent] < $pointsmax)
                        {
                                $buff[] = "Vyžaduje " . $pointsmax . " bodů v <b>" . $r[0]->name . "</b>";
                                $check = false;
                        }
                }
                
                // může se naučit, ale nemá body
                if ($this->talents == 0 && $check && $this->characterTalents[$id] < $pointsmax)
                {
                        $buff[] = "Nemáš dost bodů cest";
                }
                
                return implode("<br />", $buff);
        }
        
        public function canLearnTalent($id)
        {
                $vysledek = true;
                
                // nejprve kontrolujeme, jestli na to má dost bodů v hlavním směru
                $s = dibi::query("SELECT way, pointsinway, pointsmax
                                    FROM ways_talents 
                                   WHERE id = %i", $id);
                $r = $s->fetchAll();
                $way = $r[0]->way;
                $pointsinway = $r[0]->pointsinway;
                $pointsmax = $r[0]->pointsmax;
                
                // má dost bodů v hlavním směru?
                if ($pointsinway > 0 && (!isset($this->characterTalents[$way]) OR $this->characterTalents[$way] < $pointsinway))
                {
                        return false;
                }
                
                // nemá již maximum?
                if ($pointsmax == $this->characterTalents[$id])
                {
                        return false;
                }
                
                // má nějaké volné body?
                if ($this->talents == 0)
                {
                        return false;
                }
                
                // kontrolujeme rekurzivně jednotlivé předchozí nutné talenty
                $lasttalent = 0;
                while (true)
                {
                        $s = dibi::query("SELECT d.mother, w.pointsmax
                                            FROM ways_talents_dependence d, ways_talents w
                                           WHERE d.talent = %i", ($lasttalent == 0 ? $id : $lasttalent),
                                            "AND d.mother = w.id");
                        $r = $s->fetchAll();
                        if (empty($r))
                        {
                            break;
                        }
                        $lasttalent = $r[0]->mother;
                        $pointsmax = $r[0]->pointsmax;
                        
                        if (!isset($this->characterTalents[$lasttalent]) OR $this->characterTalents[$lasttalent] < $pointsmax)
                        {
                                $vysledek = false;
                        }
                }
                
                return $vysledek;
        }
        
        public function hasItem($id)
        {
                $s = dibi::query("SELECT COUNT(*)
                                    FROM hero_inventory 
                                   WHERE %and", array(array("hero = %i", $this->id),
                                                      array("item = %i", $id)));
                $kontrola = $s->fetchSingle();
                
                if ($kontrola)
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }
        
        public function deleteItem($id, $number)
        {
                // how many items does he have?
                $s = dibi::query("SELECT number FROM hero_inventory WHERE %and", array(array("hero = %i", $this->id),
                                                                                       array("item = %i", $id)));
                $n = $s->fetchSingle();

                if ($number == $n)
                {
                    dibi::query("DELETE FROM hero_inventory WHERE %and", array(array("hero = %i", $this->id),
                                                                               array("item = %i", $id)));
                }
                else
                {
                    dibi::query("UPDATE hero_inventory SET number = number - %i", $number, "WHERE %and", array(array("hero = %i", $this->id),
                                                                                                               array("item = %i", $id)));
                }
        }
}